"""
HiAgent使用示例
"""
import asyncio
from services.app_service import AppService
from services.favorite_service import FavoriteService
from services.personal_agent_service import PersonalAgentService
from services.plugin_service import PluginService
from services.knowledge_service import KnowledgeService
from models.hiagent_models import AppType, ListOption, ListAppCenterFilter


async def main():
    """主函数示例"""
    
    # 初始化服务
    app_service = AppService()
    favorite_service = FavoriteService()
    personal_agent_service = PersonalAgentService()
    plugin_service = PluginService()
    knowledge_service = KnowledgeService()
    
    print("=== HiAgent功能演示 ===\n")
    
    # 1. 获取应用中心分类
    print("1. 获取应用中心分类:")
    try:
        categories = app_service.list_app_center_categories()
        print(f"找到 {len(categories)} 个分类")
        for category in categories[:3]:  # 显示前3个
            print(f"  - {category.name}: {category.description}")
    except Exception as e:
        print(f"获取分类失败: {e}")
    
    print()
    
    # 2. 获取应用中心列表
    print("2. 获取应用中心列表:")
    try:
        list_opt = ListOption(page=1, page_size=5)
        filter_opt = ListAppCenterFilter(keyword="智能")
        
        apps, total = app_service.list_app_center(list_opt, filter_opt)
        print(f"找到 {total} 个应用，显示前 {len(apps)} 个:")
        for app in apps:
            print(f"  - {app.name}: {app.description}")
    except Exception as e:
        print(f"获取应用列表失败: {e}")
    
    print()
    
    # 3. 获取个人智能体列表
    print("3. 获取个人智能体列表:")
    try:
        list_opt = ListOption(page=1, page_size=5)
        agents, total = personal_agent_service.list_personal_agents(list_opt)
        print(f"找到 {total} 个个人智能体，显示前 {len(agents)} 个:")
        for agent in agents:
            print(f"  - {agent.name}: {agent.description}")
    except Exception as e:
        print(f"获取个人智能体列表失败: {e}")
    
    print()
    
    # 4. 获取插件分类
    print("4. 获取插件分类:")
    try:
        categories = plugin_service.list_available_plugin_categories()
        print(f"找到 {len(categories)} 个插件分类")
        for category in categories[:3]:  # 显示前3个
            print(f"  - {category.name}: {category.description}")
    except Exception as e:
        print(f"获取插件分类失败: {e}")
    
    print()
    
    # 5. 获取插件列表
    print("5. 获取插件列表:")
    try:
        plugins, total = plugin_service.list_plugins(page_size=5)
        print(f"找到 {total} 个插件，显示前 {len(plugins)} 个:")
        for plugin in plugins:
            print(f"  - {plugin.name}: {plugin.description}")
    except Exception as e:
        print(f"获取插件列表失败: {e}")
    
    print()
    
    # 6. 获取知识库列表
    print("6. 获取知识库列表:")
    try:
        datasets, total = knowledge_service.list_datasets(page_size=5)
        print(f"找到 {total} 个知识库，显示前 {len(datasets)} 个:")
        for dataset in datasets:
            print(f"  - {dataset.name}: {dataset.description}")
    except Exception as e:
        print(f"获取知识库列表失败: {e}")
    
    print()
    
    # 7. 创建个人智能体示例
    print("7. 创建个人智能体示例:")
    try:
        app_id = personal_agent_service.create_personal_agent(
            name="测试智能体",
            description="这是一个测试智能体",
            app_type=AppType.CHAT
        )
        if app_id:
            print(f"成功创建智能体，ID: {app_id}")
        else:
            print("创建智能体失败")
    except Exception as e:
        print(f"创建智能体失败: {e}")
    
    print()
    
    # 8. 创建知识库示例
    print("8. 创建知识库示例:")
    try:
        dataset_id = knowledge_service.create_dataset(
            name="测试知识库",
            description="这是一个测试知识库"
        )
        if dataset_id:
            print(f"成功创建知识库，ID: {dataset_id}")
        else:
            print("创建知识库失败")
    except Exception as e:
        print(f"创建知识库失败: {e}")
    
    print("\n=== 演示完成 ===")


if __name__ == "__main__":
    asyncio.run(main())